package com.ml.mall.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.page.Page;
import com.ml.mall.bean.req.sys.SysRolePermQueryDto;
import com.ml.mall.bean.req.sys.SysRolePermissionDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.sys.SysPermission;
import com.ml.mall.entity.sys.SysRolePermission;
import com.ml.mall.mapper.sys.SysPermissionMapper;
import com.ml.mall.mapper.sys.SysRolePermissionMapper;
import com.ml.mall.service.sys.ISysRolePermissionService;
import com.ml.mall.service.sys.SysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 角色权限对应关系表 服务实现类
 * </p>
 *
 * @author kyyang
 * Created 2021-01-07
 */
@Service
@Slf4j
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements ISysRolePermissionService {

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Autowired
    private SysPermissionService sysPermissionService;

    @Override
    public Page<SysPermission> sysRolePermission(SysRolePermQueryDto sysRolePermQueryDto) {
        //查询角色权限关联信息
        Integer menuCount = sysRolePermissionMapper.selectMenuCount(sysRolePermQueryDto);

        Page<SysPermission> page = new Page<>(sysRolePermQueryDto.getPageIndex(), sysRolePermQueryDto.getPageNum());
        if (menuCount < 1) {
            return page;
        }
        List<SysPermission> menuRecords = sysRolePermissionMapper.selectMenuPage(sysRolePermQueryDto);
        //查询权限信息
        page.setTotal(menuCount);
        page.setRecords(menuRecords);
        return page;
    }

    @Override
    @Transactional
    public int sysRolePermissionNew(SysRolePermissionDto sysRolePermissionDto) {
        //删除角色现有权限
        Integer roleId = sysRolePermissionDto.getRoleId();
        if(null != roleId){
            sysRolePermissionMapper.deleteByRoleIds(Stream.of(roleId).collect(Collectors.toList()));
        }

        List<Integer> menuIds = sysRolePermissionDto.getPermissionIds();
        if (null != menuIds && menuIds.size() > 0) {
            //获取菜单及菜单下权限的id
            List<Integer> permissionIds = sysPermissionService.querySubPermissionIds(menuIds);
            permissionIds.addAll(menuIds);

            //添加角色权限
            if (permissionIds.size() > 0) {
                SysRolePermissionDto rolePermissionDto = new SysRolePermissionDto();
                rolePermissionDto.setRoleId(roleId);
                rolePermissionDto.setPermissionIds(permissionIds);
                return sysRolePermissionMapper.insertBatch(rolePermissionDto);
            }
        }
        return 0;
    }

    @Override
    @Transactional
    public int sysRolePermissionDel(SysRolePermissionDto sysRolePermissionDto) {
        List<Integer> permissionIds = sysRolePermissionDto.getPermissionIds();
        if (null != permissionIds && permissionIds.size() > 0) {
            LambdaQueryWrapper<SysRolePermission> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysRolePermission::getRoleId, sysRolePermissionDto.getRoleId());
            wrapper.in(SysRolePermission::getPermissionId, permissionIds);
            return sysRolePermissionMapper.delete(wrapper);
        }
        return 0;
    }
}
